<template>
	<!-- 考试详情-->
	<view class="body">
		<view>
			<Answer  :isReviewed='isReview' currentSwiperItem='0' :duration='300'
				:questtionList='questtionList' @submit='handleSubmit' @onChange='onChange'></Answer>
		</view>
	</view>
</template>

<script>
	import NetWorkTool from '@/api/netWorkTool'
	import Answer from './../../components/dyw-answer/answer.vue'
	export default {
		data() {
			return {
				title: '考试中',
				isReview: false,
				questtionList: [],
				loadingMessage: '',
				confirmSubmitContent: '',
				errMessage: '',
				answers: []
			}
		},
		created() {
			NetWorkTool.POST('/api/question-bank/checkIsDoSubject', {}, false).then(res => {
				if (res == 1) {
					this.isReview = true
				} else {
					this.isReview = false
				}
			})
			this.init()
			
		},
		onReady() {},
		components: {
			Answer
		},
		methods: {
			init() {
				NetWorkTool.POST('/api/question-bank/queryEverydaySubject', {}, false).then(res => {
					let arr = [];
					for (let item of res) {
						let o = {};
						o.id = item.id
						o.title = item.subject
						o.answer = item.answer
						o.type = this.testEnum(item.type)
						let explainContent=  item.userAnswer == undefined ? "" : item.userAnswer
						o.explainContent = "您的答案：" + explainContent
						o.userAnswer=""
						o.difficulty = ""
						// 解析选项
						let opts = [];
						let paserObj = JSON.parse(item.options)
						let mp = new Map(Object.entries(paserObj));
						mp.forEach((value, key, mp) => {
							opts.push({
								optionId: key,
								id: key,
								content: value,
								type: this.testEnum(item.type),
								questionId: item.id
							})
						})
						o.optionList = opts
						arr.push(o)
					}
					this.questtionList = arr
				})

			},
			/* 提交答案
			 */
			handleSubmit(questtionList) {
				if (!this.isReview) {
					let array = [];
					let first = this.answers[0]
					for(let i = 1; i<= this.answers.length; i++) {
						if (this.answers[i]==undefined ||first.id != this.answers[i].id){
							array.push(first)
						}
						first = this.answers[i]
					}
					// 判断如果题没做完不可以提交
					if (array.length < 5){
						uni.showToast({
							title: '您还有题未做，请全部做完后提交！',
							icon: 'none',
						});
						return ;
					}
					// 题目编号不能为空
					let questionId = []
					// 答案列表
					let a = []
					for (let item of array) {
						questionId.push(item.id)
					}
					for (let item of array) {
						a.push(item.userAnswer)
					}
					let param = {
						"questionAnswer": a.toString(),
						"questionId": questionId.toString(),
					}
					NetWorkTool.POST('/api/question-bank/everydaySubject', param, false).then(res => {
						this.answers = []
						uni.redirectTo({
							url: './../../index/index',
						})
					})
				}else {
					uni.redirectTo({
						url: './../../index/index',
					})
				}

			},
			/* 题目答案变化
			 */
			onChange(answer) {
				this.answers.push(answer)
				console.log(answer)
			},
			testEnum(desc) {
				if (desc == '单选题') {
					return 2;
				}
				if (desc == '多选题') {
					return 3;
				}
			}

		}
	}
</script>

<style lang="scss">
	@import url("/static/party_building/assets/style/federation.css");

	page {
		background-color: #FFFFFF;
	}
</style>
